<template>
  <div class="app-page">
    <!-- 头 -->
    <div class="app-header">
      <div class="app-title">
        <i :class="$route.meta.icon"></i> {{ $route.meta.title }}
      </div>
    </div>
    <!-- 单页 -->
    <div class="app-page-padding" style="height:calc(100% - 50px)">
      <div v-loading="pageLoading" style="padding:20px; padding-left:0;">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" size="small" label-width="180px">
          <el-form-item label="区域选择" prop="filed01">
            <RegionCascader
              v-model="ruleForm.filed01"
              :props="{ checkStrictly: true }"
              placeholder="大区/区域/场区"
              size="medium"
              style="width:500px"
              @change="onChange"
            >
            </RegionCascader>
          </el-form-item>
          <el-form-item label="" prop="filed20">
            <el-button :loading="loading" type="primary" size="small" class="fa fa-check" @click="validateSubmit(ruleForm)">
              保存
            </el-button>
            <el-button size="small" class="fa fa-reply-all" @click="close">
              关闭
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { getData, update } from '@/mock/index'

import RegionCascader from '@/components/RegionCascader/RegionCascader.vue'
export default {
  name: 'FormBase',
  components: {
    RegionCascader
  },
  data () {
    return {
      // 提交表单loading
      loading: false,
      // 页面加载完成loading
      pageLoading: false,
      // 表单
      ruleForm: {
        field09: []
      },
      // 验证
      rules: {
      }
    }
  },
  created () {
    this.initForm()
  },
  methods: {
    onChange (e) {
      console.log(e, 111)
    },
    initForm () {
      // 用户uuid
      const { uuid } = this.$route.query
      if (uuid) {
        this.uuid = uuid
        this.getDetail(uuid)
      }
    },
    // 获取编辑数据
    async getDetail (uuid) {
      // 模拟详情接口
      const detailData = {
        filed01: '',
        filed02: '',
        filed09: []
      }
      this.pageLoading = true
      const res = await getData({ userUuid: uuid }, detailData)
      this.pageLoading = false
      if (res.code === '0000') {
        this.ruleForm = res.data
      }
    },
    // 关闭跳转
    close () {
      this.closeView(this.$route)
    },
    // 验证
    validateSubmit (ruleForm) {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          this.submit(ruleForm)
        } else {
          return false
        }
      })
    },
    // 提交表单
    async submit (ruleForm) {
      this.loading = true
      // 编辑
      const res = await update(ruleForm)
      this.loading = false

      if (res.code === '0000') {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.jumpRouterView(this.$route, { path: '/user' })
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style  lang="scss" scoped>
</style>
